home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8206 / 8206.xpi / content / overlay.js < prev    next >
Text File  |  2010-02-02  |  9KB  |  252 lines

  1. var WisestampOverlay = 
  2. {
  3.     app: null,
  4.     _prefs: null,
  5.     get prefs() {
  6.         if (!this._prefs) {
  7.             this._prefs = Components.classes["@mozilla.org/preferences-service;1"].
  8.             getService(Components.interfaces.nsIPrefService);
  9.             this._prefs = this._prefs.getBranch("extensions.wisestamp.");
  10.         }
  11.         return this._prefs;
  12.     },
  13.  
  14.     onLoad: function () 
  15.     {
  16.         // initialization code
  17.         this.initialized = true;
  18.         this.setSignatureType();
  19.         this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
  20.         this.prefs.addObserver("", this, false);
  21.  
  22.         this.app = WisestampGetAppObject();
  23.         this.app.onLoad();
  24.  
  25.         this.updateEnabledState();
  26.         this.updateStatusbarState();
  27.  
  28.         this.checkVersionUpdate();
  29.         
  30.         WiseStampUtils.log("[overlay.js::onLoad] Version = "+WiseStampUtils.getVersion()+", app = "+WiseStampUtils.getAppType()+" ver. "+WiseStampUtils.getAppVersion());  
  31.         WiseStampComm.writeLog(WiseStampComm.LOG_EVENT_INIT, 
  32.             WiseStampUtils.getVersion(), WiseStampUtils.getAppType(), WiseStampUtils.getAppVersion(),
  33.             WiseStampPrefs.getBoolPref("promote"));
  34.  
  35.         function wisestampWebListener(e) {
  36.             alert("data:" + e.target.getAttribute("signatureContent"));
  37.         }
  38.         document.addEventListener("WisestampExtensionEvent", wisestampWebListener, false, true);
  39.     },
  40.  
  41.     onUnload: function () {
  42.         this.initialized = false;
  43.         this.prefs.removeObserver("", this);
  44.         document.removeEventListener("ShowWisestampMenu", this, false, true);
  45.         var context = document.getElementById("contentAreaContextMenu");
  46.         if (context) context.removeEventListener("popupshowing", this, false);
  47.     },
  48.  
  49.     checkVersionUpdate: function () {
  50.         var currentVersion = WiseStampUtils.getVersion();
  51.         var lastVersion = this.prefs.getCharPref("currentVersion");
  52.  
  53.         if (lastVersion == "none") {
  54.             this.prefs.setCharPref("currentVersion", currentVersion);
  55.             //var firstrunURL = "http://www.wisestamp.com/first-steps".replace("{previous}","").replace("{current}",currentVersion);
  56.             var firstrunURL = "http://www.wisestamp.com/first-steps";
  57.             window.setTimeout(function () {
  58.                 WisestampOverlay.app.openFirstRun();
  59.             },
  60.             3000);
  61.         } else if (lastVersion != currentVersion) {
  62.             this.prefs.setCharPref("currentVersion", currentVersion);
  63.             /* any upgrade code goes here */
  64.             window.setTimeout(function () {
  65.                 WisestampOverlay.app.openUpdate(currentVersion);
  66.             },
  67.             3000);
  68.         }
  69.     },
  70.  
  71.     setSignatureType: function (aType, aChangePref, popup) {
  72.         var current = this.prefs.getCharPref("type");
  73.         if (!aType) aType = current;
  74.         this.onPopupShowing(null, popup);
  75.         if (aChangePref && current != aType) {
  76.             this.prefs.setCharPref("type", aType);
  77.         }
  78.     },
  79.  
  80.     onMenuItemCommand: function (event) {
  81.         this.onEditSignature(event);
  82.     },
  83.  
  84.     onUseSignature: function (event, aType, aPopup) {
  85.         WiseStampUtils.log("[overlay.js::onUseSignature] >>>>> ");
  86.         this.setSignatureType(aType, false, aPopup);
  87.         this.app.onUseSignature(event, aType);
  88.     },
  89.  
  90.     onEditSignature: function (event) {
  91.         /* Open options window */
  92.         var optionsURL = "chrome://wisestamp/content/options.xul";
  93.         var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  94.         var windows = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator).getEnumerator(null);
  95.         while (windows.hasMoreElements()) {
  96.             var win = windows.getNext();
  97.             if (win.document.documentURI == optionsURL) {
  98.                 win.focus();
  99.                 return;
  100.             }
  101.         }
  102.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2);
  103.         var features;
  104.         try {
  105.             var instantApply = prefs.getBoolPref("browser.preferences.instantApply");
  106.             instantApply = true;
  107.             features = "chrome,titlebar,toolbar,centerscreen" + (instantApply ? ",dialog=no" : ",modal");
  108.         } catch(e) {
  109.             features = "chrome,titlebar,toolbar,centerscreen,modal";
  110.         }
  111.         openDialog(optionsURL, "", features);
  112.     },
  113.  
  114.     onStatusClicked: function (event) {
  115.         if (event.button == 0) {
  116.             //this.toggleState();
  117.         }
  118.     },
  119.  
  120.     toggleState: function () {
  121.         this.prefs.setBoolPref("enabled", !this.enabled);
  122.     },
  123.  
  124.     onPopupShowing: function (event, popup) {
  125.         if (!document.getElementById("wisestampStatusPopup")) 
  126.             return false;
  127.  
  128.         if (typeof WisestampCompose != "undefined") 
  129.         {
  130.             if (!this.prefs.getBoolPref("enabled")) 
  131.                 return false;
  132.         }
  133.  
  134.         var current = event ? WisestampSignatureFactory.lastInserted : null;
  135.         if (!current) 
  136.             current = this.prefs.getCharPref("type");
  137.  
  138.         if (!popup) 
  139.             popup = "";
  140.  
  141.         if (popup == "") {
  142.             var enableItem = document.getElementById("wisestamp-enable");
  143.             var disableItem = document.getElementById("wisestamp-disable");
  144.             var target = document.popupNode;
  145.             if (target && target.id == "wisestamp-statusbar") 
  146.                 disableItem.hidden = !(enableItem.hidden = this.enabled);
  147.             else 
  148.                 disableItem.hidden = enableItem.hidden = true;
  149.         }
  150.  
  151.         var signatures = WiseStampUtils.getSignaturesFromPref();
  152.         var container, menuseparator;
  153.         if (popup == "-menu") {
  154.             container = document.getElementById("context-wisestamp").firstChild;
  155.             menuseparator = document.getElementById("wisestamp_context_separator");
  156.         } else if (popup == "") {
  157.             container = document.getElementById("wisestampStatusPopup");
  158.             menuseparator = document.getElementById("wisestamp_popup_separator");
  159.         }
  160.         
  161.         if (menuseparator) 
  162.         {
  163.             /* clear out existing entries */
  164.             while (container.firstChild.id != menuseparator.id) {
  165.                 container.removeChild(container.firstChild);
  166.             }
  167.  
  168.             function buildSigPopupMenuitem(label, sig) 
  169.             {
  170.                 sig = sig ? sig : encodeURIComponent(label.replace(/^\s+|\s+$/, "").toLowerCase());
  171.                 var menuitem = document.createElement("menuitem");
  172.                 menuitem.setAttribute("id", "wisestamp" + popup + "-type-" + sig);
  173.                 menuitem.setAttribute("label", label);
  174.                 menuitem.setAttribute("value", sig);
  175.                 menuitem.setAttribute("type", "checkbox");
  176.                 if (current == sig || ((current == "" || !current) && sig == "none")) 
  177.                     menuitem.setAttribute("checked", "true");
  178.                     
  179.                 var objname = typeof WisestampCompose != "undefined" ? "WisestampCompose" : "WisestampOverlay";
  180.                 menuitem.setAttribute("oncommand", objname + ".onUseSignature(event, '" + sig + "', '" + popup + "');")
  181.                 return menuitem;
  182.             }
  183.  
  184.             for each(var label in signatures) {
  185.                 var menuitem = buildSigPopupMenuitem(label);
  186.                 container.insertBefore(menuitem, menuseparator);
  187.             }
  188.  
  189.             if (popup == "") {
  190.                 var menuitem = buildSigPopupMenuitem("None", "none");
  191.                 container.insertBefore(menuitem, menuseparator);
  192.             }
  193.         }
  194.  
  195.         return true;
  196.     },
  197.  
  198.     updateEnabledState: function () {
  199.         this.enabled = this.prefs.getBoolPref("enabled");
  200.         var statusbar = document.getElementById("wisestamp-statusbar");
  201.         if (statusbar) {
  202.             statusbar.setAttribute("state", this.enabled ? "enabled" : "disabled");
  203.         }
  204.         if (this.enabled) {
  205.             this.app.enable();
  206.         } else this.app.disable();
  207.     },
  208.     updateStatusbarState: function () {
  209.         var statusbar = document.getElementById("wisestamp-statusbar");
  210.         if (statusbar) {
  211.             var hidestatusbar = this.prefs.getBoolPref("hidestatusbar");
  212.             statusbar.hidden = hidestatusbar;
  213.         }
  214.     },
  215.     hideToolbarButton: function () {
  216.         //dump("val: "+this.prefs.getBoolPref("hidebuttons")+"\n");
  217.         var hide = this.prefs.getBoolPref("hidebuttons");
  218.         var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
  219.         var enumerator = wm.getEnumerator(null);
  220.         while (enumerator.hasMoreElements()) {
  221.             var win = enumerator.getNext();
  222.             if (win.location.toString() == "chrome://messenger/content/messengercompose/messengercompose.xul") {
  223.                 //dump("uri: "+win.location+"\n");
  224.                 var bttn = win.document.getElementById("wisestampButton");
  225.                 bttn.setAttribute("hidden", hide);
  226.             }
  227.             // win is [Object ChromeWindow] (just like window), do something with it  
  228.         }
  229.     },
  230.     observe: function (subject, topic, data) 
  231.     {
  232.         if (topic != "nsPref:changed") 
  233.             return;
  234.             
  235.         switch (data) 
  236.         {
  237.         case "enabled":
  238.             this.updateEnabledState();
  239.             break;
  240.         case "hidestatusbar":
  241.             this.updateStatusbarState();
  242.             break;
  243.         case "hidebuttons":
  244.             this.hideToolbarButton();
  245.             break;
  246.         }
  247.     }
  248. };
  249.  
  250. window.addEventListener("load", function (e) { WisestampOverlay.onLoad(e); }, false);
  251. window.addEventListener("unload", function (e) { WisestampOverlay.onUnload(e); }, false);
  252.